package com.cci.kangdao.utilTool;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期类
 */
public class Date_Util {
    public static Date now() {
        return Calendar.getInstance().getTime();
    }

    public static String toString(Date d) {
        return toString(d, "yyyy-MM-dd HH:mm:ss");
    }

     /**
      * @description: 获取当前时间
      * @param:
      * @return:
      * @exception:
      * @author: shiliqiang
      * @company: CTY Technology Co.,Ltd
      * @since: 2021/7/23 14:25
      * @version: V1.0
      */
    public static String getNowTime() {
        //获取当前时间
        return LocalTimeUtil.getRealCurrentTime();
    }

    public static String toString(Date d, String fmt) {
        if (null == d)
            return "";

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);
            return sdf.format(d);
        } catch (Exception e) {

        }
        return d.toString();
    }

    public static Date toDate(String d, String fmt) {
        if (null == d)
            return new Date();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);
            return sdf.parse(d);
        } catch (Exception e) {

        }
        return new Date();
    }

    /**
     * 得到二个日期间的间隔分钟数
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return 开始时间和结束时间之间相差的分钟数
     */
    public static int getMinutes(Date begin, Date end) {
        long m = 0;

        try {
            m = (end.getTime() - begin.getTime()) / (60 * 1000);
            if (m<0)
                m=(-1)*m;
        } catch (Exception e) {
            return 0;
        }

        return (int) m;
    }

    public static String addDay(int days) {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        Date trialTime = new Date();
        calendar.setTime(trialTime);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return myFormatter.format(calendar.getTime());
    }

    public static String addDay(String date, int days) {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        Date trialTime = new Date();
        try {
            trialTime = myFormatter.parse(date);
        } catch (ParseException e) {
        }
        calendar.setTime(trialTime);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return myFormatter.format(calendar.getTime());
    }

    public static String subDay(int days) {
        return addDay(-days);
    }

    public static String subDay(String date, int days) {
        return addDay(date, -days);
    }

    public static String addMonth(int month) {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        Date trialTime = new Date();
        calendar.setTime(trialTime);
        calendar.add(Calendar.MONTH, month);
        return myFormatter.format(calendar.getTime());
    }

    public static String subMonth(int month) {
        return addMonth(-month);
    }


    /**
     * 传入类型标志符 得到日期
     *
     * @return
     * @throws ParseException
     */
    public static String GetTime(int i, int old) {
        DateFormat dateform = DateFormat.getDateInstance();
        Date date = new Date();
        String today = dateform.format(date);
        today = addDay(today, old);
        switch (i) {
            case 1: // 今天日期如：2016-08-30
                break;
            case 2: // 今天日期如：20160830
                today = today.replace("-", "");
                break;
            case 3: // 当月如：201608
                today = today.replace("-", "").substring(0, 6);
                break;
            case 4: // 时间戳如：2017-05-06 08:36:23
                today = toString(date);
                break;
            case 5: // 昨天日期如：20160830
                today = Integer.valueOf(today.replace("-", "")) - 1 + "";
                break;
            case 6: // 今天日期(去掉0)如：2016-8-30
                SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-M-d");
                today = myFormatter.format(date);
                break;
            case 7: // 今天日期(去掉0)如：2016-8-30
                today = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(date);
                break;

            default:
                break;
        }
        return today;
    }


    /**
     * 通过时间戳得到时间
     *
     * @param ltime
     * @return
     */
    public static String GetTimeByTimesTamp(long ltime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String today = format.format(ltime);

        return today;
    }

    /**
     * 通过时间获取日期
     */
    public static String getOnlyDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format2 = format.format(date);
        return format2;
    }


    /**
     * 通过时间获取日期
     */
    public static String getOnlyDate(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format2 = format.format(toDate(date,"yyyy-MM-dd"));
        return format2;
    }

    /**
     * 通过时间得到时间戳
     *
     * @param
     * @return
     */
    public static long GetTimesTampByLTime(String Time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(Time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 方法说明：计算两个时间差用秒数表示
     * <br/>参数：两个时间
     * <br/>返回值：十分秒的时间
     */
    public static int getSeconds(String s_date, String e_date) {
        int miaoshu = 0;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (s_date.length() == 10) {
                s_date = s_date + " 00:00:00";
            }
            if (e_date.length() == 10) {
                e_date = e_date + " 23:59:59";
            }

            long s_date_long = formatter.parse(s_date).getTime();
            long e_date_long = formatter.parse(e_date).getTime();

            miaoshu = (int) ((e_date_long - s_date_long) / 1000);

            //如果有余数
            if (miaoshu % 2 == 1) {
                miaoshu = miaoshu + 1;
            }

        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return miaoshu;
    }

    /**
     * 获取两个时间的时间差
     *
     * @param st   开始时间
     * @param et   结束时间
     * @param type 类型（1：秒，2：分，3：小时，4：天）
     * @return
     */
    public static long getSubTime(String st, String et, int type) {
        if (StringUtils.isBlank(st) || StringUtils.isBlank(et)) {
            return -1;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (st.length() == 10) {
            st = st + " 00:00:00";
        }
        if (et.length() == 10) {
            et = et + " 23:59:59";
        }
        try {
            long s_date_long = sdf.parse(st).getTime();
            long e_date_long = sdf.parse(et).getTime();
            //获取秒数
            if (type == 1) {
                return (e_date_long - s_date_long) / 1000;
            } else if (type == 2) {
                return (e_date_long - s_date_long) / (1000 * 60);
            } else if (type == 3) {
                return (e_date_long - s_date_long) / (1000 * 60 * 60);
            } else if (type == 4) {
                return (e_date_long - s_date_long) / (1000 * 60 * 60 * 24);
            } else {
                return 0;
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 增加小时
     */
    public static String addHour(String time ,int hour){
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = new GregorianCalendar();
        Date trialTime = null;
        try {
            trialTime = myFormatter.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        calendar.setTime(trialTime);
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return myFormatter.format(calendar.getTime());
    }

}
